package com.ruoyi.web.controller.goods;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.goods.vo.CategoryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.goods.domain.Category;
import com.ruoyi.goods.service.ICategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物品分类信息Controller
 *
 * @author yeqi
 * @date 20 22-11-27
 */
@RestController
@RequestMapping("/goods/category")
public class CategoryController extends BaseController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询物品分类信息列表
     */
    @PreAuthorize("@ss.hasPermi('goods:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(Category category) {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }

    /**
     * 查询所有物品分类信息列表
     */
    @PreAuthorize("@ss.hasPermi('goods:category:list')")
    @GetMapping("/all")
    public AjaxResult listAll(Category category)
    {

        List<CategoryVo> list = categoryService.TreeList();
        return AjaxResult.success(list);
    }

    /**
     * 导出物品分类信息列表
     */
    @PreAuthorize("@ss.hasPermi('goods:category:export')")
    @Log(title = "物品分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, list, "物品分类信息数据");
    }

    /**
     * 获取物品分类信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(categoryService.getById(id));
    }

    /**
     * 新增物品分类信息
     */
    @PreAuthorize("@ss.hasPermi('goods:category:add')")
    @Log(title = "物品分类信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(categoryService.save(category));
    }

    /**
     * 修改物品分类信息
     */
    @PreAuthorize("@ss.hasPermi('goods:category:edit')")
    @Log(title = "物品分类信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {

        Category ordCategory= categoryService.getById(category.getId());
        if (ordCategory.getParentId() == null){
            if (category.getParentId() != null){
                throw new ServiceException("上级列表不能修改");
            }
        }else {
            if(!ordCategory.getParentId().equals(category.getParentId())){
                throw new ServiceException("上级列表不能修改");
            }
        }

        return toAjax(categoryService.updateById(category));
    }

    /**
     * 删除物品分类信息
     */
    @PreAuthorize("@ss.hasPermi('goods:category:remove')")
    @Log(title = "物品分类信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {

        return toAjax(categoryService.delectById(id));
    }
}
